package com.sunny.core.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class CommonDateOper {
	/**
	* 
	*
	* @return  yyyy-MM-dd HH:mm:ss
	*/
	public static java.sql.Date getNowsqlDate() {
		java.util.Date currentTime = new java.util.Date();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		java.sql.Date date=new java.sql.Date(currentTime.getTime());
		
		return date;
	}

	
	public static java.util.Date getNowDate(){
		
		Calendar c = Calendar.getInstance(Locale.CHINA);
		
		return c.getTime();
		
	}

	/**
	* 
	*
	* @return yyyy-MM-dd HH:mm:ss
	*/
	public static String getStringDate() {
		java.util.Date currentTime = new java.util.Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	/**
	 * 
	* 
	*
	* @returnyyyy-MM-dd
	*/
	public static String getStringDateShort() {
		java.util.Date currentTime = new java.util.Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = formatter.format(currentTime);
		return dateString;
	}
	/**
	 * õǰڼ
	 * */
	public static String getSeqWeek() {
		Calendar c = Calendar.getInstance(Locale.CHINA);
		String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
		if (week.length() == 1)
		   week = "0" + week;
		String year = Integer.toString(c.get(Calendar.YEAR));
		return year + week;
		}
	public static void main(String[] args){
		CommonDateOper d=new CommonDateOper();
		String ss=d.getSeqWeek();
		//System.out.println(ss);
	}
	/**
	 * õǰڼ
	 * */
	public static int dayofweek(){
		 Calendar ca = Calendar.getInstance();
	     int day = ca.get(Calendar.DAY_OF_WEEK);//
	     return day;
	}
	
	
	
	
	/**
	 * ڵ
	 * */
	public static int getnowyear(){
		 Calendar ca = Calendar.getInstance();
	      int year = ca.get(Calendar.YEAR);//ȡ
	      return year;

	}
	/**
	 * ڵ·
	 * */
	public static int getnowmonth(){
		 
	      Calendar ca = Calendar.getInstance();
	     
	      int month=ca.get(Calendar.MONTH);//ȡ· 
	      return month;
	}
	/**
	 * 
	 * @param dateString
	 * @return
	 * @throws java.text.ParseException
	 */
	public final static java.sql.Timestamp string2Time(String dateString)
	   throws java.text.ParseException {
	   DateFormat dateFormat;
	   dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);//趨ʽ
	   dateFormat.setLenient(false);
	   java.util.Date timeDate = dateFormat.parse(dateString);//util
	   java.sql.Timestamp dateTime = new java.sql.Timestamp(timeDate.getTime());//Timestamp,timeDate.getTime()һlong
	   return dateTime;

	}

	/**
	 * 
	 * @param dateString
	 * @return
	 */
	public static Date StringToDateTime(String dateString)
	{
		Date time=null;
		
		java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
			 time = sdf.parse(dateString);
		} catch (ParseException e) {
		}
		
		return time;
	}
	
	/**
	 * 
	 * @param dateString
	 * @return
	 */
	public static java.sql.Date StringToDate(String dateString)
	{
		java.util.Date time=null;
		java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd");
        try {
			 time = sdf.parse(dateString);
		} catch (ParseException e) {
		}
		
		return new java.sql.Date(time.getTime());
	}
	
	
	/**
	 * 
	 * @param dateString
	 * @return
	 */
	public static java.util.Date StringToUtilDate(String dateString)
	{
		java.util.Date time=null;
		java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd");
        try {
        	
			 time = sdf.parse(dateString);
			 
		} catch (ParseException e) {
		
		}
		
		return new java.util.Date(time.getTime());
	}
	
	public static java.sql.Date StringToDate1(String dateString)
	{
		java.util.Date time=null;
		java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
			 time = sdf.parse(dateString);
			// //System.out.println(time);
		} catch (ParseException e) {
		}
		
		return new java.sql.Date(time.getTime());
	}
	
	public static String DateToString(java.sql.Date date){
		if(date==null) return "";
		java.util.Date time=new java.util.Date(date.getTime());
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = formatter.format(time);
		return dateString;
		
	}
	public static int getAge(Date birthDay) throws Exception {
		  Calendar cal = Calendar.getInstance();
		  if (cal.before(birthDay)) {
		   throw new IllegalArgumentException(
		     "The birthDay is before Now.It's unbelievable!");
		  }
		  int yearNow = cal.get(Calendar.YEAR);
		  int monthNow = cal.get(Calendar.MONTH);
		  int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
		  cal.setTime(birthDay);// 
		  int yearBirth = cal.get(Calendar.YEAR);
		  int monthBirth = cal.get(Calendar.MONTH);
		  int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
		  int age = yearNow - yearBirth;
		  if (monthNow <= monthBirth) {
		   if (monthNow == monthBirth) {
		    // monthNow==monthBirth
		    if (dayOfMonthNow < dayOfMonthBirth) {
		     age--;
		    } else {
		     // do nothing
		    }
		   } else {
		    // monthNow>monthBirth
		    age--;
		   }
		  } else {
		   // monthNow<monthBirth
		   // donothing
		  }
		  return age;

	}
	
	
}
